<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="h_title">
        <img src="../assets/cangku.png" alt />
        <div>仓管系统</div>
      </div>
      <div class="h_info">
          欢迎您，<i class="el-icon-user-solid"></i> {{userInfo.username}}
          <!-- <el-tooltip class="item" effect="dark" content="退出登录" placement="bottom">
            <el-button round size="small" class="el-icon-switch-button loguot" @click="logout"></el-button>
          </el-tooltip> -->
          <el-dropdown>
          <span class="el-dropdown-link">
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <router-link :to="{ path: '/institution'}" replace>仓库管理制度</router-link>
            </el-dropdown-item>
            <el-dropdown-item divided>
              <span class="loguot" @click="logout">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '66px' : '202px'">
        <div class="toggle-button" style="font-size:20px" @click="toggleCollapse">
          <i class="el-icon-s-unfold"></i>
        </div>
        <!-- 侧边栏菜单区域 -->
        <!--unique-opened 展开唯一一项子菜单-->
        <!--:collapse=ture表示折叠-->
        <el-menu
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="this.$router.history.current.path"
        >
          <!-- :default-active="this.$route.history.current.path" -->
          <!-- 一级菜单 -->
          <el-submenu v-for="(item,index) in menulist" :key="index" :index="index + ''">
            <!-- 设置v-for语句并未每个item绑定一个key值，这里用id -->
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <!-- <i v-if="icons[item.id]" class="iconfont" :class='icons[item.id]'></i> -->
              <i v-if="item.icon !== null" class="iconfont" :class='item.icon'></i>
              <i v-else class="iconfont icon-wuliaoguanli1"></i>
              <!-- 文本 -->
              <span>{{item.name}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="(subItem,indexs) in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <!-- <i v-if="subIcons[index][subItem.id]" class="iconfont" :class="subIcons[index][subItem.id]"></i> -->
                <i v-if="subItem.icon !== null" class="iconfont" :class="subItem.icon"></i>
                <i v-else class="iconfont icon-wuliaoguanli1"></i>
                <!-- 文本 -->
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      userInfo:{},
    }
  },
  created () {
    this.listMenus()
    this.getuserInfo()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async listMenus () {
      const { data: res } = await this.$http.get('/menu/list/tree')
      if (res.status !== 200) return this.$message.error(res.message)
      this.menulist = res.data
      console.log(this.menulist)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    changePwd () {
      this.$router.push('/passwords')
    },
    // 获取当前用户信息
    async getuserInfo(){
      const { data: res } = await this.$http.get('/account/info')
      if (res.status !== 200) return this.$message.error(res.message)
      this.userInfo = res.data
      console.log(this.userInfo)
    },
  }
}
</script>

<style>
a {
    text-decoration: none;
    color:black;
}
.router-link-active {
    text-decoration: none;
    color:black;
}
</style>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  img {
    height:3rem;
    background-color:#fff;
    border-radius:50%;
    margin-right: 1.5rem;;
  }
  background-color: #333854;
  display: flex;
  justify-content: space-between;
  // // padding: 0 224px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  .h_title {
    display: flex;
    align-items: center;
  }
  .h_info {
    font-size: 16px;
    .loguot {
      margin-left: 1rem;
    }
  }
}

.el-aside {
  background-color: #fff;
  border: 1px #dfe1e6 solid;
  .el-menu {
    border-right: none;
    color: #252b3a;
  }
}

.el-main {
  // 主要内容区
  background-color: #eef0f5;
}

// 间距十像素
.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #fff;
  font-size: 10px;
  line-height: 24px;
  color: #252b3a;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
